#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from twisted.internet import reactor, protocol
from iFriends import *
import time
import thread

dispatcher = {}
test_sequence = []
test_state = 0
seq = 2

def Send_GetUserInfo(client) :
	global seq
	req = SocialFriendsSyn()
	req.count = seq

#	for i in range(seq):
	req.user_id[0] = "뚜르비용2"
	req.user_id[1] = "뚜르비용3"
	
	client.send(req)
	print 'send'
	#seq += 1
	
def Send_MainList(client) :
	req = MainListSyn()
	req.seq = 1
	client.send(req)
	print 'MainList'
	
def Send_BuffTimeSyn(client) :
	req = BuffTimeSyn()
	req.seq = 1
	client.send(req)
	print 'BuffTime'
	
def Send_RecommendSyn(client) :
	req = RecommendSyn()
	req.seq = 1
	client.send(req)
	print 'Recommend'
	
def Read_GetUserInfo(client, recv_data) :
	ack = SocialFriendsAck()
	ack.Load(recv_data)
	print '============================================================'
	print 'count : ' + str(ack.count)	
	for i in range(ack.count):
		print 'result : ' + str(ack.array_Friends[i].user_id)	
		print 'result : ' + str(ack.array_Friends[i].seq)	
	print '============================================================'
	
	time.sleep(1)
	Send_GetUserInfo(client)

def Send_1(client):
	Send_GetUserInfo(client)
	Send_BuffTimeSyn(client)
	Send_RecommendSyn(client)
	Send_MainList(client)
#	client.send();

test_sequence.append(Send_GetUserInfo)
test_sequence.append(Send_BuffTimeSyn)
test_sequence.append(Send_RecommendSyn)
test_sequence.append(Send_MainList)
#dispatcher[SocialFriendsAck.MSG_ID] = Read_GetUserInfo
#test_sequence.append(Send_RegisterUser_Req)
#dispatcher[MsgRouter_SendMsg_Ntf.MSG_ID] = Recv_SendMsg_Ntf

def console(threadName, client) :
	while True :
		value = input('msg id :')
		if (value == 1):
			#receiver = input('user_seq :')
			#Send_UserInfoSyn(client, receiver)
			Send_1(client)
		elif (value == 503):
			user_seq = input('user_seq :')
			char_seq = input('char_seq :')
			Send_CharInfoSyn(client, user_seq, char_seq)
		elif (value == 505):
			user_seq = input('user_seq :')
			char_seq = input('char_seq :')
			Send_FlushCharacterInfoSyn(client, user_seq, char_seq)
		elif (value == 507):
			user_seq = input('user_seq :')
			Send_FlushUserInfoSyn(client, user_seq)
		elif (value == 509):
			user_seq = input('user_seq :')
			slot_seq = input('slot_seq :')
			Send_FlushUserSlotInfoSyn(client, user_seq, slot_seq)
		elif (value == 511):
			user_seq = input('user_seq :')			
			Send_RemoveUser(client, user_seq)

class Client(protocol.Protocol): 
	recv_data = ''
	def connectionMade(self): 
		global test_sequence, test_state
		#test_sequence[test_state](self)
		thread.start_new_thread(console, ('AA',self))

	def connectionLost(self, reason): 	
		print "connection lost" 

	def dataReceived(self, data) :
		# length of total(include header) : 4byte
		# tag_idl : 4byre
		# data 
		return
		self.recv_data += data
		while len(self.recv_data) :
			global dispatcher, test_sequence, test_state
			if(len(self.recv_data) < 6) :
				print 'received data size is under \'length size\''
				break
		
			recv_bytes_tobe = int(struct.unpack('h', self.recv_data[1:3])[0]) + 3
			if(len(self.recv_data) < recv_bytes_tobe) :
				print 'received data size is under \'total size\''
				break;

			tag_id = struct.unpack('h', self.recv_data[4:6])[0]; 
			recv_data = self.recv_data[6:recv_bytes_tobe] 
			self.recv_data = self.recv_data[recv_bytes_tobe:]
			dispatcher[tag_id](self, recv_data)
			#test_state += 1
			#if test_state < len(test_sequence) :
			#	test_sequence[test_state](self)
			
	def send(self, msg) :
		data = msg.Store()
		send_data  = struct.pack('B', 0xff)
		send_data += struct.pack('h', 3 + len(data))
		send_data += struct.pack('b', 0)
		send_data += struct.pack('h', msg.MSG_ID)
		send_data += data
		self.transport.write(send_data)

class ClientFactory(protocol.ClientFactory): 
	protocol = Client 
	def clientConnectionFailed(self, connector, reason): 
		print "Connection failed - goodbye!" 
		reactor.stop() 
	def clientConnectionLost(self, connector, reason): 
		print "Connection lost - goodbye!" 
		reactor.stop() # this connects the protocol to a server runing on port 8000 

def main(): 
	f = ClientFactory() 
	reactor.connectTCP("localhost", 20001, f) 
	#reactor.connectTCP("192.168.200.73", 20001, f) 
	reactor.run() 

if __name__ == '__main__': 
	main() 
